﻿using System.Windows.Forms;

namespace CloudShot.Utils
{
	public static class RegistryUtils
	{
		/// <summary>
		/// Add/Remove registry entries for windows startup.
		/// </summary>
		/// <param name="AppName">Name of the application.</param>
		/// <param name="enable">if set to <c>true</c> [enable].</param>
		public static void SetStartup(string AppName, bool enable)
		{
			string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

			Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(runKey);

			if (enable)
			{
				if (startupKey.GetValue(AppName) == null)
				{
					startupKey.Close();
					startupKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(runKey, true);
					// Add startup reg key
					startupKey.SetValue(AppName, Application.ExecutablePath);
					startupKey.Close();
				}
			}
			else
			{
				// remove startup
				startupKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(runKey, true);
				startupKey.DeleteValue(AppName, false);
				startupKey.Close();
			}
		}
	}
}
